package org.mtmi.ui.links;

import org.eclipse.swt.graphics.Point;
import org.mtmi.ui.controls.IVisualControl;

/**
 * Provide oval outline point, as if given point is connected to center of control's bounds.
 * Oval is included in control's bounds.
 */
public class OvalConnectionProvider implements IConnectionOutlinePointProvider
{
	private IVisualControl control;
	
	public OvalConnectionProvider( IVisualControl c ) {
		control = c;
	}
	
	@Override
	public Point getOutlinePoint( int x, int y ) 
	{
		Point l = control.getLocation();
		Point s = control.getSize();
		int rx = s.x/2;
		int ry = s.y/2;
		int cx = l.x+rx;
		int cy = l.y+ry;
		int dx = x-cx, dy = y-cy;
		double angle = Math.atan2( -dy, dx );
		//outline point:
		int ox = (int)Math.round( cx+rx*Math.cos(angle) );
		int oy = (int)Math.round( cy-rx*Math.sin(angle) );
		
		int dox = ox-cx, doy = oy-cy;
		double d_o = Math.sqrt( dox+doy );
		double d_p = Math.sqrt( dx*dx+dy*dy );
		if( d_p < d_o ) {
			//point is inside oval: return it:
			return new Point( x, y );
		} else {
			return new Point( ox, oy );
		}
	}

}
